package org.jeecg.modules.workflow.convert.definition;


import com.fasterxml.jackson.core.type.TypeReference;
import org.jeecg.common.util.json.JsonUtils;
import org.jeecg.common.workflow.vo.definition.rule.BpmTaskAssignRuleCreateReqVO;
import org.jeecg.common.workflow.vo.definition.rule.BpmTaskAssignRuleRespVO;
import org.jeecg.common.workflow.vo.definition.rule.BpmTaskAssignRuleUpdateReqVO;
import org.jeecg.modules.workflow.entity.BpmTaskAssignRule;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class BpmTaskAssignRuleConvertImpl implements BpmTaskAssignRuleConvert {

    private static final TypeReference<Set<String>> typeReference = new TypeReference<Set<String>>(){};

    @Override
    public BpmTaskAssignRuleRespVO convert(BpmTaskAssignRule bean) {
        if ( bean == null ) {
            return null;
        }

        BpmTaskAssignRuleRespVO bpmTaskAssignRuleRespVO = new BpmTaskAssignRuleRespVO();

        bpmTaskAssignRuleRespVO.setType( bean.getType() );
        Set<String> set = JsonUtils.parseObject(bean.getOptions(), typeReference);
        if ( set != null ) {
            bpmTaskAssignRuleRespVO.setOptions( new HashSet<String>( set ) );
        }
        bpmTaskAssignRuleRespVO.setId( bean.getId() );
        bpmTaskAssignRuleRespVO.setModelId( bean.getModelId() );
        bpmTaskAssignRuleRespVO.setProcessDefinitionId( bean.getProcessDefinitionId() );
        bpmTaskAssignRuleRespVO.setTaskDefinitionKey( bean.getTaskDefinitionKey() );

        return bpmTaskAssignRuleRespVO;
    }

    @Override
    public BpmTaskAssignRule convert(BpmTaskAssignRuleCreateReqVO bean) {
        if ( bean == null ) {
            return null;
        }

        BpmTaskAssignRule bpmTaskAssignRuleDO = new BpmTaskAssignRule();

        bpmTaskAssignRuleDO.setModelId( bean.getModelId() );
        bpmTaskAssignRuleDO.setTaskDefinitionKey( bean.getTaskDefinitionKey() );
        bpmTaskAssignRuleDO.setType( bean.getType() );
        Set<String> set = bean.getOptions();
        if ( set != null ) {
            bpmTaskAssignRuleDO.setOptions( JsonUtils.toJsonString(set) );
        }

        return bpmTaskAssignRuleDO;
    }

    @Override
    public BpmTaskAssignRule convert(BpmTaskAssignRuleUpdateReqVO bean) {
        if ( bean == null ) {
            return null;
        }

        BpmTaskAssignRule bpmTaskAssignRuleDO = new BpmTaskAssignRule();

        bpmTaskAssignRuleDO.setId( bean.getId() );
        bpmTaskAssignRuleDO.setType( bean.getType() );
        Set<String> set = bean.getOptions();
        if ( set != null ) {
            bpmTaskAssignRuleDO.setOptions( JsonUtils.toJsonString(set) );
        }

        return bpmTaskAssignRuleDO;
    }

    @Override
    public List<BpmTaskAssignRule> convertList2(List<BpmTaskAssignRuleRespVO> list) {
        if ( list == null ) {
            return null;
        }

        List<BpmTaskAssignRule> list1 = new ArrayList<BpmTaskAssignRule>( list.size() );
        for ( BpmTaskAssignRuleRespVO bpmTaskAssignRuleRespVO : list ) {
            list1.add( bpmTaskAssignRuleRespVOToBpmTaskAssignRuleDO( bpmTaskAssignRuleRespVO ) );
        }

        return list1;
    }

    protected BpmTaskAssignRule bpmTaskAssignRuleRespVOToBpmTaskAssignRuleDO(BpmTaskAssignRuleRespVO bpmTaskAssignRuleRespVO) {
        if ( bpmTaskAssignRuleRespVO == null ) {
            return null;
        }

        BpmTaskAssignRule bpmTaskAssignRuleDO = new BpmTaskAssignRule();

        bpmTaskAssignRuleDO.setId( bpmTaskAssignRuleRespVO.getId() );
        bpmTaskAssignRuleDO.setModelId( bpmTaskAssignRuleRespVO.getModelId() );
        bpmTaskAssignRuleDO.setProcessDefinitionId( bpmTaskAssignRuleRespVO.getProcessDefinitionId() );
        bpmTaskAssignRuleDO.setTaskDefinitionKey( bpmTaskAssignRuleRespVO.getTaskDefinitionKey() );
        bpmTaskAssignRuleDO.setType( bpmTaskAssignRuleRespVO.getType() );
        Set<String> set = bpmTaskAssignRuleRespVO.getOptions();
        if ( set != null ) {
            bpmTaskAssignRuleDO.setOptions( JsonUtils.toJsonString(set) );
        }

        return bpmTaskAssignRuleDO;
    }
}
